|
In programming languages, the associativity (or fixity) of an operator is a property that determines how operators of the same precedence are grouped in the absence of parentheses. If an operand is both preceded and followed by operators (for example, "^ 4 ^"), and those operators have equal precedence, then the operand may be used as input to two different operations (i.e. the two operations indicated by the two operators). The choice of which operations to apply the operand to, is determined by the "associativity" of the operators. Operators may be associative (meaning the operations can be grouped arbitrarily), left-associative (meaning the operations are grouped from the left), right-associative (meaning the operations are grouped from the right) or non-associative (meaning operations can not be chained, often because the output type is incompatible with the input types). The associativity and precedence of an operator is a part of the definition of the programming language; different programming languages may have different associativity and precedence for the same type of operator. Consider the expression a ~ b ~ c . If the operator ~ has left associativity, this expression would be interpreted as (a ~ b) ~ c . If the operator has right associativity, the expression would be interpreted as a ~ (b ~ c) . If the operator is non-associative, the expression might be a syntax error, or it might have some special meaning. Some mathematical operators have inherent associativity. For example, subtractionand division, as used in conventional math notation, are inherently left-associative. Addition and multiplication, by contrast, have no inherent associativity, though most programming languages define an associativity for these operations as well. Many programming language manuals provide a table of operator precedence and associativity; see, for example, the table for C and C++. The concept of notational associativity described here is related to, but different from the mathematical associativity. An operation that is mathematically associative, by definition requires no notational associativity. (For example, addition has the associative property, therefore it does not have to be either left associative or right associative.) An operation that is not mathematically associative, however, must be notationally left-, right-, or non-associative. (For example, subtraction does not have the associative property, therefore it must have notational associativity.) == Examples == Associativity is only needed when the operators in an expression have the same precedence. Usually + and - have the same precedence. Consider the expression 7 − 4 + 2 . The result could be either (7 − 4) + 2 = 5 or 7 − (4 + 2) = 1 . The former result corresponds to the case when + and − are left-associative, the latter to when + and - are right-associative.In order to reflect normal usage, addition, subtraction, multiplication, and division operators are usually left-associative〔 while an exponentiation operator (if present) is right-associative; this applies to the up-arrow operator as well. Any assignment operators are also typically right-associative. To prevent cases where operands would be associated with two operators, or no operator at all, operators with the same precedence must have the same associativity. 抄文引用元・出典: フリー百科事典『 ウィキペディア(Wikipedia)』 ■ウィキペディアで「Operator associativity」の詳細全文を読む スポンサード リンク
|